﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using VehicleManagement.Codes;
using VehicleManagement.Models;
using System.Collections;

namespace VehicleManagement.ControllersServices
{
    public class ApprovalServices
    {
        CarInfoServices carServices = new CarInfoServices();
        DriverServices driverServices = new DriverServices();

        /// <summary>
        /// 根据车ID查车牌号，司机ID查司机姓名
        /// </summary>
        /// <param name="approval">审批表信息对象</param>
        /// <returns>修改后的审批表信息对象</returns>
        public Approval GetDriverNameAndCarNumber(ref Approval approval)
        {
            approval.CarNumber = carServices.GetCarNumByCarId(approval.CarID);
            approval.DriverName = driverServices.GetDriverNameByDriverID(approval.DriverID);

            return approval;
        }

        /// <summary>
        /// 根据车牌号查车ID，司机姓名查司机ID
        /// </summary>
        /// <param name="approval">审批表信息对象</param>
        /// <returns>修改后的审批表信息对象</returns>
        public Approval GetDriverIDAndCarID(ref Approval approval)
        {
            approval.CarID = carServices.GetCarIdByCarNum(approval.CarNumber);
            approval.DriverID = driverServices.GetDriverIdByDriverName(approval.DriverName);

            return approval;
        }


        /// <summary>
        /// 查找所有审批表，负责人权限
        /// </summary>
        /// <returns>审批列表</returns>
        public IList<Approval> GetAllApprovals()
        {
            IList<Approval> lstApproval = SqlMapperManager.Instance.Get().QueryForList<Approval>("SELECT_ALL_APPROVAL", null);
            return lstApproval;
        }

        /// <summary>
        /// 根据申请表ID查找某个审批表，负责人权限
        /// </summary>
        /// <param name="id">申请表ID</param>
        /// <returns>审批表信息对象</returns>
        public Approval GetApprovalByApplyID(int id)
        {
            IList<Approval> lstApproval = SqlMapperManager.Instance.Get().QueryForList<Approval>("SELECT_APPROVAL_BY_APPLYID", id);
            Approval approval = lstApproval.Where(u => u.ApplyID == id).First();

            GetDriverNameAndCarNumber(ref approval);

            return approval;
        }

        /// <summary>
        /// 根据编号显示某个审批表
        /// </summary>
        /// <param name="id">审批表ID</param>
        /// <returns>审批表信息对象</returns>
        public Approval GetApprovalByApprovalID(int id)
        {
            IList<Approval> lstApproval = SqlMapperManager.Instance.Get().QueryForList<Approval>("SELECT_APPROVAL_BY_APPROVALID", id);
            Approval approval = lstApproval.Where(u => u.ApprovalID == id).First();

            if (approval.CarID != 0 && approval.DriverID != 0)
                GetDriverNameAndCarNumber(ref approval);
            else
            {
                approval.CarNumber = "";
                approval.DriverName = "";
            }
            return approval;
        }

        public int IsApprovalExist(int id)
        {
            int hasApproval = 0;

            hasApproval = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_APPROVALID_BY_APPLYID", id));

            return hasApproval;
        }

        /// <summary>
        /// 到审批表查询某条申请是否通过
        /// </summary>
        /// <param name="id">申请表ID</param>
        /// <returns></returns>
        public bool GetApplyState(int id)
        {
            bool applyState = Convert.ToBoolean(SqlMapperManager.Instance.Get().QueryForObject("SELECT_APPLYSTATE_BY_APPLYID",id));
            return applyState;
        }

        /// <summary>
        /// 到审批表查询状态是否通过
        /// </summary>
        /// <param name="id">审批表ID</param>
        /// <returns></returns>
        public bool GetApprovalState(int id)
        {
            bool approvalState = Convert.ToBoolean(SqlMapperManager.Instance.Get().QueryForObject("SELECT_APPLYSTATE_BY_APPROVALID", id));
            return approvalState;
        }

        /// <summary>
        /// 增加一个审批
        /// </summary>
        /// <param name="id">审批表信息对象</param>
        /// <returns>所增加条目的主键</returns>
        public object CreateApproval(Approval approval)
        {
            GetDriverIDAndCarID(ref approval);

            return SqlMapperManager.Instance.Get().Insert("INSERT_APPROVAL", approval);
        }


        /// <summary>
        /// 修改一个审批
        /// </summary>
        /// <param name="id">审批表信息对象</param>
        /// <returns>修改列数</returns>
        public int EditApproval(Approval approval)
        {
            GetDriverIDAndCarID(ref approval);
            if (approval.CarID > 0 && approval.DriverID > 0)
            {
                //修改司机状态为不空闲
                driverServices.UpdateDriverState(approval.DriverID, 1);

                //修改车辆状态为不空闲
                carServices.UpdateCarState(approval.CarID, 1);

                //修改审批表状态为已通过
                approval.ApplyState = true;
                //ChangeApprovalState(approval.ApprovalID, true);

                //通过的申请信息插入行程主表
                SqlMapperManager.Instance.Get().Insert("INSERT_TRACK_NOW", approval.ApprovalID);
            }
            else
                approval.ApplyState = false;

            return SqlMapperManager.Instance.Get().Update("UPDATE_APPROVAL", approval);
        }

        /// <summary>
        /// 修改审批表状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private int ChangeApprovalState(int approvalID, bool approvalState)
        {
            Hashtable ht = new Hashtable();
            ht.Add("approvalID", approvalID);
            ht.Add("approvalState", approvalState);
            return SqlMapperManager.Instance.Get().Update("UPDATE_APPROVALSTATE", ht);
        }

        /// <summary>
        /// 删除一个审批
        /// </summary>
        /// <param name="id">审批表信息对象</param>
        /// <returns>删除列数</returns>
        public int DeleteApproval(int id)
        {
            return SqlMapperManager.Instance.Get().Delete("DELETE_APPROVAL", id);
        }
    }
}